package com.relax.mybatis.plugin.sensitive.strategy.impl;

import org.apache.commons.lang3.StringUtils;

import com.relax.mybatis.plugin.sensitive.strategy.SensitiveStrategy;

/**
 * @author relax
 * @date 2022/07/05
 * @desc 银行卡号脱敏策略
 */
public class BankCardSensitiveStrategy implements SensitiveStrategy {

    /**
     * <p>银行卡号脱敏：显示前四后四位</p>
     *
     * <pre>
     * BankCardSensitiveStrategy.handle("12341234")         = "12341234"
     * BankCardSensitiveStrategy.handle("1234567812345678") = "1234********5678"
     * </pre>
     *
     * @param data
     * @param symbol
     * @return
     */
    @Override
    public String handle(String data, String symbol) {
        int length = StringUtils.length(data);
        if (length <= 8) {
            return data;
        }
        return StringUtils.rightPad(StringUtils.left(data, 4), length - 4, symbol)
                .concat(StringUtils.right(data, 4));
    }

}
